package org.jash.imdemo.fragment;

import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hyphenate.chat.EMContact;

import org.jash.imdemo.R;

import java.util.ArrayList;
import java.util.List;

public class ContactAdapter  extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {
    private List<String> data = new ArrayList<>();
    private OnContactClickListener listener;
    @NonNull
    @Override
    public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ContactViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ContactViewHolder holder, int position) {
        String contact = data.get(position);
        holder.icon.setImageResource(R.mipmap.ic_launcher_round);
        holder.name.setText(contact);
        if (listener != null) {
            holder.itemView.setOnClickListener(v -> listener.onContactClick(contact));
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void addAll(List<String> list) {
        int size = data.size();
        data.addAll(list);
        notifyItemRangeInserted(size, list.size());
    }

    public void setListener(OnContactClickListener listener) {
        this.listener = listener;
    }

    public interface OnContactClickListener {
        void onContactClick(String name);
    }
    public static class ContactViewHolder extends RecyclerView.ViewHolder {
        private ImageView icon;
        private TextView name;
        public ContactViewHolder(@NonNull View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.icon);
            name = itemView.findViewById(R.id.name);
        }
    }
}
